//index.js
const app = getApp()
import {
  GoodsModels
} from "../../utils/models/goods/Goods-models";
import {
  ConfigModels
} from "../../utils/models/config/Config-models";
var model = new GoodsModels();
var configModels = new ConfigModels();
Page({
  data: {
    navbarHeight: app.globalData.navbarHeight,
    screenHeight: '',
    height: '',
    titleName: '蒲尽广佛',
    navBgColor: "yellow",
    isLeft: false,
    activeIndex: 0,
    // classList: [{
    //   "id": 1,
    //   "title": '全部'
    // }, {
    //   "id": 2,
    //   "title": '下午茶/甜品'
    // }, {
    //   "id": 3,
    //   "title": '异地风味'
    // }, {
    //   "id": 4,
    //   "title": '自助餐/放题'
    // }, {
    //   "id": 5,
    //   "title": '休闲娱乐'
    // }],
    centerClass:[],
    prefectureList:[],
    autoplay: true,
    indicatorDots: true,
    interval: 5000,
    duration: 500,
    current: 0,
    swiperList: [{
      images: '/images/test/swiper1.png',
      url: '/',
      title: '测试1'
    }, {
      images: '/images/test/swiper1.png',
      url: '/',
      title: '测试2'
    }, {
      images: '/images/test/swiper1.png',
      url: '/',
      title: '测试3'
    }],
    recommendList: [{
      images: '/images/test/swiper-1.png',
      url: '/',
      title: '嘉信城市广场吃货店优惠大放送'
    }, {
      images: '/images/test/swiper-1.png',
      url: '/',
      title: '嘉信城市广场吃货店优惠大放送2'
    }, {
      images: '/images/test/swiper-1.png',
      url: '/',
      title: '嘉信城市广场吃货店优惠大放送3'
    }],
    isMask: true,
    tel: '13217504708',
    kftel: '13217504708',
    wx: 'bxina7',
    kfwx: 'bxina8',
    ruzhuModal: false,
    kefuModal: false,
    isMask: false,
    page: 1,  //分页
    cid:0,  
    goodsList:[],
    gifList: [
      { 'id':1,'cover': '/images/test/test1.png'},
      { 'id':2,'cover': '/images/test/test1.png'},
      { 'id':3,'cover':'/images/test/test1.png'}
    ]
  },

  // menu高度
  getswiper_H() {
    let query = wx.createSelectorQuery();
    const windowHeight = wx.getSystemInfoSync().windowHeight
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight
    const screenHeight = wx.getSystemInfoSync().screenHeight
    const navbarHeight = app.globalData.navbarHeight
    query.select('.nav').boundingClientRect(rect => {
        let height = rect.height;
        this.setData({
            nav_H: height,
            navbarHeight,
            windowHeight,
            statusBarHeight,
            screenHeight
        })
    }).exec();
},

  // 点击顶部菜单
  onMenu: function (e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var id = parseInt(e.currentTarget.dataset.id);
    var args = {
      'cid':id,
      'page':1
    }
    this.setData({
      goodsList:[]
    })
    if (index > 0){
      this.getGoodsList(args)
    }
    this.setData({
      activeIndex: index,
      page:1,
      cid:id
    });
  },

  //获取导航高度
  getNavHeight() {
    let query = wx.createSelectorQuery();
    query.select('.nav').boundingClientRect(rect => {
      let height = rect.height;
      this.setData({
        NavHeight: height
      })
    }).exec();
  },

  // current 改变时会触发
  changeCurrent: function (e) {
    this.setData({
      current: e.detail.current
    });
  },

  // 拨打手机号码
  phoneCall: function (e) {
    var tel = e.currentTarget.dataset.tel;
    if (tel) {
      wx.makePhoneCall({
        phoneNumber: tel,
        success: function (res) {

        },
        fail: function (res) {
          console.log('拨打失败')
        }
      })
    }

  },

  // 一键复制事件
  copyBtn: function (e) {
    var data = e.currentTarget.dataset.wx;
    if (wx) {
      wx.setClipboardData({
        //准备复制的数据
        data: data,
        success: function (res) {
          console.log(res)
        }
      });
    }
  },

  //入驻商家
  ruzhuBtn: function() {
    this.setData({
      isMask:true,
      ruzhuModal: true
    });
  },

  // 联系客服
  kefuBtn: function() {
    this.setData({
      isMask:true,
      kefuModal: true
    });
  },

  // 关闭弹窗
  closeModal: function() {
    this.setData({
      isMask: false,
      ruzhuModal:false,
      kefuModal: false
    });
  },
  getIndex: function () {
    let that = this;
    model.getIndex(function (res) {
      var data = res.data;
      // console.log(data.recommendList.banner_list);
      that.setData({
        classList: data.classes.headerArr,
        swiperList:data.bannerList,
        centerClass:data.classes.centerArr,
        recommendList: data.recommendList.banner_list,
        gifList: data.recommendList.gif_list,
      })
    })
  },
  isBottom(){
    let page = this.data.page;
    let activeIndex = this.data.activeIndex;
    page += 1;
    if (activeIndex == 0){
      this.getPrefecture(page)
    }else{
      let cid = this.data.cid;
      let args = {
        'cid':cid,
        'page':page
      };
      this.getGoodsList(args)
    }
    this.setData({
      'page': page
    })
    
  },
  //获取专区
  getPrefecture:function(page){
    let that = this;
    var prefectureList = this.data.prefectureList;
    model.getPrefecture(page,function(res){
      if(res.code > 0){
        var data = res.data;
        var prefecture = {
          'title': data.area.title,
          'desc': data.area.desc,
          'goodsList': data.goods_list
        }
        prefectureList.push(prefecture);
        console.log(`prefectureList`,prefectureList)
        that.setData({
          prefectureList: prefectureList
        })
      }
    })
  },
  //获取首页商品
  getGoodsList:function(args){
    let that = this;
    let goodsList = this.data.goodsList;
    model.getGoodsList(args, function (res) {
      if(res.code > 0){
        for(var i in res.data){
          goodsList.push(res.data[i]);
        }
      }
      that.setData({
        'goodsList':goodsList
      })
    })
  },
  onLoad: function () {
    this.getswiper_H();
    let page = this.data.page;
    this.getPrefecture(page);
  },

  onShow: function () {
    this.getNavHeight();
    this.getIndex();
    var that = this;
    configModels.getConfig(function(res){
      console.log(res);
      that.setData({
        kftel:res.data.tel,
        kfwx:res.data.wx,
        tel:res.data.tel,
        wx:res.data.wx
      })
    })
  },

  

})